package com.lvmama.rhino.web.comment.reform.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lvmama.rhino.pojo.comment.Enum.EnumForCategory;
import com.lvmama.rhino.pojo.comment.entity.vo.ReformListVO;
import com.lvmama.rhino.pojo.comment.request.ReformListRequest;
import com.lvmama.rhino.service.comment.ReformListService;
import com.lvmama.rhino.service.impl.comment.RedisServiceImpl;
import com.lvmama.rhino.utils.Constants;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 差评整改 2017-10-24
 */

@Controller
@RequestMapping("/reform")
public class ReformListController {

    private Logger log = Logger.getLogger(ReformListController.class);

    @Autowired
    private ReformListService reformListService;

    @Autowired
    private RedisServiceImpl redisService;

    /**
     * 跳转到差评整改页并查询列表
     *
     * @param model
     * @return page
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String reformList(ModelMap model) {
        try {
            log.info("reform list start");
            // 品类
            EnumForCategory[] categories = EnumForCategory.getAllCategory();
            model.put("categories", categories);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("reform list error:" + e.getMessage(), e);
        }
        log.info("reform list end");
        return "application/reform/reformList";
    }

    /**
     * 差评整改
     * 7天内的数据查询redis(第一页&无查询条件)
     *
     * @param request
     * @return
     */
    private boolean isReformListQueryRedis(ReformListRequest request) {
        if (StringUtils.isBlank(request.getCategoryId()) && StringUtils.isBlank(request.getProductId())
                && 1 == request.getCurrentPage()) {
            return true;
        }
        return false;
    }

    /**
     * 查询差评列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "reformList", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo<ReformListVO> getReformList(ReformListRequest request) {
        PageInfo<ReformListVO> reformList = new PageInfo<>();
        Object reformListObj;
        try {
            String key = redisService.getKey("reformList");
            log.info("reform reformList start");
            if (isReformListQueryRedis(request)) {
                log.info("reform reformList redis start");
                reformListObj = redisService.getObjectValue(key);
                if (null != reformListObj) {
                    String jsonStr = reformListObj.toString();
                    reformList = new Gson().fromJson(jsonStr, new TypeToken<PageInfo<ReformListVO>>() {}.getType());
                    log.info("reform reformList redis end");
                } else {
                    reformList = queryReformList(request);
                    redisService.setValueExpire(key, reformList, Constants.TIMEOUT);
                }
            } else {
                reformList = queryReformList(request);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("reform reformList redis error:" + e.getMessage(), e);
            try{
                reformList = queryReformList(request);
            } catch (Exception e1) {
                log.error("reform reformList mysql requery error:" + e1.getMessage(), e1);
                e1.printStackTrace();
            }
        }
        log.info("reform reformList end");
        return reformList;
    }

    /**
     * 查询差评整改列表
     * @param request
     * @return PageInfo<ReformListVO> q
     */
    private PageInfo<ReformListVO> queryReformList(ReformListRequest request) throws Exception {
        log.info("reform reformList queryReformList start");
        PageInfo<ReformListVO> reformList = reformListService.getReformList(request);
        transReformEnum(reformList);
        log.info("reform reformList queryReformList end");
        return reformList;
    }

    /**
     * 枚举转化
     *
     * @param reformList
     */
    private void transReformEnum(PageInfo<ReformListVO> reformList) throws Exception {
        List<ReformListVO> reformListVOList = reformList.getList();
        if (!CollectionUtils.isEmpty(reformListVOList)) {
            for (int i = 0; i < reformListVOList.size(); i++) {
                ReformListVO reformListVO = reformListVOList.get(i);
                // 品类名称
                String categoryName = Constants.UNKNOWN;
                String categoryId = reformListVO.getCategoryId();
                if (!Constants.UNKNOWNDATA.equals(categoryId)) {
                    categoryName = StringUtils.isNotBlank(categoryId) ? EnumForCategory.getCategoryShowName(categoryId) : null;
                    if(StringUtils.isBlank(categoryName)) {
                        categoryName = EnumForCategory.getCategoryName(categoryId);
                    }
                }
                reformListVO.setCategoryName(categoryName);

                // 产品名称
                String productName = reformListVO.getProductName();
                if (StringUtils.isBlank(productName)) {
                    productName = Constants.UNKNOWN;
                } else if (Constants.UNKNOWNDATA.equals(productName.trim()) ||
                        Constants.UNKNOWNDATA2.equals(productName.trim()) ||
                        Constants.UNKNOWNDATA3.equalsIgnoreCase(productName.trim())) {
                    productName = Constants.UNKNOWN;
                }
                reformListVO.setProductName(productName);
            }
        }
    }


}
